package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.InquiryGoodDto;
import com.authine.cloudpivot.ext.dto.PageObject;
import com.authine.cloudpivot.ext.enums.inquiry.InquiryStatusEnum;
import com.authine.cloudpivot.ext.utils.BoResultFormat;
import com.authine.cloudpivot.ext.utils.BoServiceUtils;
import com.authine.cloudpivot.ext.utils.Constant;
import com.authine.cloudpivot.ext.utils.InquiryGoodJdbc;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.dto.bo.query.ListBOQuery;
import com.authine.mvp.app.launcher.dto.bo.query.expression.Operator;
import com.authine.mvp.app.launcher.dto.bo.query.expression.SimpleCondition;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;
@Slf4j
@Path("InquiryGoodService")
public class InquiryGoodService extends ApplicationService {


    @Path("queryInquiryGoodList")
    @POST
    @Operation(summary = "今日新询,报价中（通过子表筛选主表，而列表全是主表字段 平台方法不能去重）",tags = CUSTOM_SERVICE)
    public SingleResponse<BOList> queryInquiryGoodList(@Parameter InquiryGoodDto good){
        PageObject<Map<String,Object>> pageObject = new PageObject<Map<String,Object>>();
        List<Map<String, Object>> orderList = new ArrayList<>();
        int count = 0;
        try{
            Integer pageCurrent = good.getPageCurrent() == null ?  1: good.getPageCurrent();
            Integer pageSize = good.getPageSize() == null ? 10 : good.getPageSize();
            // 设置当前页
            good.setPageCurrent(pageCurrent);
            // 设置每页显示的条数
            pageObject.setPageSize(pageSize);
            // 设置总记录数

            count = InquiryGoodJdbc.getCountSql(good);
            log.info("总条数:{}"+count);
            pageObject.setRowCount(count);
            pageObject.setPageCount(0);
            if (count != 0 ) {
                // 设置总页数
                int pageNum = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
                pageObject.setPageCount(pageNum);
                // 设置每页显示数据集合
                // 开始的记录数
                int start = (pageCurrent - 1) * pageSize;
                System.out.println(start + "-" + pageSize);
                orderList = InquiryGoodJdbc.getOrderList(good, start, pageSize);
                if (orderList != null && orderList.size() > 0) {
                    orderList.forEach(item -> {
                        String inquiry_goods_status = (String) item.get(Constant.INQUIRY_GOOD_STATUS);
                        if (StringUtils.isNotBlank(inquiry_goods_status)) {
                            item.put(Constant.INQUIRY_GOOD_STATUS, InquiryStatusEnum.get(inquiry_goods_status).getDes());
                        }
                    });
                }
            }
        }catch (Exception e){
            log.info("查询出错",e);
        }

        BoResultFormat.resultFormat(orderList, ModelConst.T_INQUIRY_GOODS);
        BOList bOList = new BOList();
        bOList.setData(orderList);
        bOList.setTotal(count);
        bOList.setSchemaCode(ModelConst.T_INQUIRY_GOODS);
        return com.alibaba.cola.dto.SingleResponse.of(bOList);
    }

    @Path("queryInquiryGoodClarifyNote")
    @POST
    @Operation(summary = "询价商品澄清记录（通过子表筛选主表，而列表全是主表字段 平台方法不能去重）",tags = CUSTOM_SERVICE)
    public SingleResponse queryInquiryGoodClarifyNote(@Parameter InquiryGoodDto good){

        PageObject<Map<String,Object>> pageObject = new PageObject<Map<String,Object>>();
        List<Map<String, Object>> orderList = new ArrayList<>();
        int count = 0;
        try{
            Integer pageCurrent = good.getPageCurrent() == null ?  1: good.getPageCurrent();
            Integer pageSize = good.getPageSize() == null ? 10 : good.getPageSize();
            // 设置当前页
            good.setPageCurrent(pageCurrent);
            // 设置每页显示的条数
            pageObject.setPageSize(pageSize);
            // 设置总记录数

            count = InquiryGoodJdbc.getInquiryGoodClarifyCount(good);
            log.info("总条数:{}"+count);
            pageObject.setRowCount(count);
            pageObject.setPageCount(0);
            if (count != 0 ) {
                // 设置总页数
                int pageNum = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1;
                pageObject.setPageCount(pageNum);
                // 设置每页显示数据集合
                // 开始的记录数
                int start = (pageCurrent - 1) * pageSize;
                System.out.println(start + "-" + pageSize);
                orderList = InquiryGoodJdbc.getInquiryGoodClarifyNote(good, start, pageSize);
            }
        }catch (Exception e){
            log.info("查询出错",e);
        }

        BoResultFormat.resultFormat(orderList, ModelConst.T_INQUIRY_GOODS);
        BOList bOList = new BOList();
        bOList.setData(orderList);
        bOList.setTotal(count);
        bOList.setSchemaCode(ModelConst.T_INQUIRY_GOODS);
        return com.alibaba.cola.dto.SingleResponse.of(bOList);

    }




}
